﻿#include "camera.h"
#include "camerammind.h"
#include "cameratest.h"
#include <mmind/MechEyeApi.h>
#include <opencv2/opencv.hpp>
#include <QDir>
#include <QFileInfo>
#include <QTime>


#include "common_log.h"

Camera::Camera()
{}

Camera::~Camera()
{}
Camera* Camera::GetCameraObject(const CameraBrand& brand)
{
	Camera* res = nullptr;
	switch (brand)
	{
	case CameraBrand::camera_brand_test:
		log_trace("相机款型：模拟相机");
		res = new CameraTest();
		break;
	case CameraBrand::camera_brand_mmind:
		log_trace("相机款型：梅卡曼德");
		res = new CameraMMind();
		break;

	default:
		log_error("相机款型：不支持的类型 {}", (int)brand);
		break;
	}

	return res;
}
int Camera::CameraInit(const CameraParam& param)
{
	// 清理过期文件夹
	QDir dir(param.path);
	if (!dir.exists()) {
		return 0;
	}
	QDateTime current = QDateTime::currentDateTime();
	// 第一阶段：扫描文件
	dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
	QFileInfoList entries = dir.entryInfoList();
	// 第二阶段：删除过期文件
	foreach(const QFileInfo & info, entries) {
		if (info.lastModified().daysTo(current) > 3) {
			if (info.isDir()) {
				QDir(info.absoluteFilePath()).removeRecursively();
			} else {
				QFile::remove(info.absoluteFilePath());
			}
		}
	}

	return 0;
}


QStringList Camera::GetCameraList()
{
	QStringList ret;
	ret << CameraMMind::GetCameraList();
	ret << CameraTest::GetCameraList();
	return ret;
}
